package com.marvels.approve.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.marvels.approve.manager.dao.IFlowNodeDao;
import com.marvels.approve.manager.dto.FlowNodeDTO;
import com.marvels.approve.manager.service.IFlowNodeService;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.exception.MarvelsBusinessException;
@Service
public class FlowNodeServiceImpl implements IFlowNodeService {
	@Autowired
	private IFlowNodeDao flowNodeDao ;
	
	@Override
	public List<FlowNodeDTO> findFlowNode(FlowNodeDTO flowNodeDTO) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		flowNodeDTO = flowNodeDTO == null?new FlowNodeDTO():flowNodeDTO;
		if(flowNodeDTO.getPageNum()<1){
			flowNodeDTO.setPageNum(PageConstant.CURRPAGE);
		}
		if(flowNodeDTO.getPageSize()<1){
			flowNodeDTO.setPageSize(PageConstant.PAGESIZE);
		}
		PageHelper.startPage(flowNodeDTO.getPageNum(), flowNodeDTO.getPageSize());
		List<FlowNodeDTO> datas = this.flowNodeDao.select(flowNodeDTO);
		return datas;
	}

	@Override
	public int saveFlowNode(FlowNodeDTO node) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		if(node.getId()==null){//新增
			//j检查该节点是否已经存在
			FlowNodeDTO flowNodeDTO = new FlowNodeDTO();
			flowNodeDTO.setNodeCode(node.getNodeCode());
			List datas = this.flowNodeDao.select(flowNodeDTO);
			if(datas!=null && datas.size() != 0){
				throw new MarvelsBusinessException(node.getNodeCode()+"已经存在!");
			}
			node.setCreatedBy(node.obtainLoginUserName());
			node.setUpdatedBy(node.obtainLoginUserName());
			return this.flowNodeDao.insert(node);
		}else{//更新
			FlowNodeDTO dbNode = this.flowNodeDao.selectByPrimaryKey(node.getId());
			if(node.getNodeCode() == null || !node.getNodeCode().equals(dbNode.getNodeCode())){
				throw new MarvelsBusinessException(node.getNodeCode()==null?"":node.getNodeCode()+"节点代码不能修改已经存在!");
			}
			node.setUpdatedBy(node.obtainLoginUserName());
			return this.flowNodeDao.updateByPrimaryKeySelective(node);
		}
	}

	public IFlowNodeDao getFlowNodeDao() {
		return flowNodeDao;
	}

	public void setFlowNodeDao(IFlowNodeDao flowNodeDao) {
		this.flowNodeDao = flowNodeDao;
	}
	
	

}
